#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：HiflyVideoAPI_Test 
@File    ：upload_url.py
@Author  ：beibei
@Date    ：2025/6/25 3:28 PM 
'''
import json
import unittest
from datetime import datetime
from pathlib import Path

import requests

from business.voices.vo import BusinessRe
from common.assertCommon import AssertCommon
from common.yamlLoader import YamlRead
from utils.utils_file import load_json_from_project, get_file_path
from common.caseOutput import info, class_case_decoration
from utils.utils_base import save_json_response


@class_case_decoration
class UploadFileTest(unittest.TestCase):

    def setUp(self):
        self.ac = AssertCommon()
        self.env_config = YamlRead().env_config1()
        self.host = self.env_config['host']
        self.authorization = self.env_config['Authorization']
        self.br = BusinessRe()

    def testCase01_V2_create_upload_url_mp4(self):
        """获取获取上传地址上传MP4地址"""
        # 用例操作步骤
        info('【step】创建声音')
        url = self.host + f'/api/v2/hifly/tool/create_upload_url'
        body = {
            "file_extension": "mp4"
        }
        res = self.br.post(url, self.authorization, body)
        info('【step】断言 code == 0')
        self.ac.code_assert("video/mp4", res.json()["content_type"])

        save_json_response(res.json(), "data/response/upload_url/mp4.json")

    def testCase02_V2_upload_file_mp4(self):
        """将文件上传到 upload_url"""
        data = load_json_from_project("data/response/upload_url/mp4.json")
        upload_url = data["upload_url"]
        content_type = data["content_type"]
        file_path = get_file_path("data/request/other/1.mp4")
        """读取mp4上传链接并上传文件"""
        headers = {
            "Content-Type": content_type
        }

        with open(file_path, 'rb') as f:
            response = requests.put(upload_url, headers=headers, data=f)

        print("上传响应状态码:", response.status_code)
        self.assertIn(response.status_code, [200, 204], "上传失败")

    def testCase03_V2_avatar_fileid(self):
        """创建视频数字人-使用file_id"""
        info('获取file_id')
        data = load_json_from_project("data/response/upload_url/mp4.json")
        file_id = data["file_id"]

        info('【step】创建数字人')
        url = self.host + f'/api/v2/hifly/avatar/create_by_video'
        body = {
            "title": "api_file_id创建视频数字人_" + datetime.now().strftime("%Y%m%d_%H%M%S"),
            "file_id": file_id
        }
        res = self.br.post(url, self.authorization, body=body)

        info('【step】断言 code == 0')
        self.ac.code_assert(0, res.json()["code"])
        save_json_response(res.json(), "data/response/avatar/avatar_new_video_file_id_v2.json")

    def testCase04_V2_avatar_error(self):
        """创建视频数字人-异常不传file_id ，返回11"""

        info('【step】创建数字人')
        url = self.host + f'/api/v2/hifly/avatar/create_by_video'
        body = {
            "title": "api_file_id创建视频数字人_" + datetime.now().strftime("%Y%m%d_%H%M%S")
        }
        res = self.br.post(url, self.authorization, body=body)

        info('【step】断言 code == 11')
        self.ac.code_assert(11, res.json()["code"])



if __name__ == '__main__':
    unittest.main()



